<?php

$html_head_code['flot_lib'] = '<script type="text/javascript" src="' . __ADMIN_URL__ . substr(dirname(__FILE__) . '/flot/jquery.flot.js', strlen(__ADMIN_DIR__)) . '"/>';

$return = $WideSql->getFromTable("WideDown",NULL,NULL,"CreatedDate","ASC");
if(!$return){
    $return = $WideSql->getError();

} else {
    foreach($return as &$r){
        $data[$r["Category"]][] = $r;
    }

    $dates = dates_inbetween($return[0]['CreatedDate'],time());
    $meta['first'] = $return[0]['CreatedDate'];
    foreach($data as $key => $d){
        $progress = 0;
        $done = 0;
        foreach($d as $e){
            $dates[date("dmy", $e['CreatedDate'])]['create']++;
            if(!empty($e['DoneDate'])){
                $dates[date("dmy", $e['DoneDate'])]['done']++;
                $done++;
            } else {
                if(!empty($e['BegunDate'])){
                    $progress++;
                    $dates[date("dmy", $e['BegunDate'])]['begun']++;
                }
            }

        }
        $meta[$key] = array (
            'total' => count($d),
            'in_progress' => $progress,
            'done' => $done
            );

    }
}
$i = 0;

foreach($dates as $k => $d){
    if(is_array($d)){
        foreach($d as $q => $val){
            if($q!="unix"){
                $status[$q] += $val;
                $unix = $d['unix']*1000;
                $flot[$q][] = "[$unix,$status[$q]]";
            }
        }
        $i++;
    }
}
foreach($flot as &$s){
    $s = "[".implode(",",$s)."]";
}
$smarty->assign("flot",$flot);
$smarty->assign("meta",$meta);
$smarty->assign("data", $data);
return $smarty->fetch('module.tpl');
function dates_inbetween($date1, $date2){

    $day = 60*60*24;
    $numbdates = ($date2-$date1)/$day + 1;
    for($i=0;$i<=$numbdates;$i++){
        $unix = $date1+($day*$i);
        $dates[date("dmy",$unix)] = array('done'=>0,'create'=>0,'begun'=>0, 'unix'=>$unix);
    }

    return $dates;
}
?>
